package com.csx.governance.traffic.condition;

import com.csx.governance.traffic.condition.constant.CompareOperator;
import com.csx.governance.traffic.condition.constant.ExpressionComponentType;

import java.util.List;
import java.util.Locale;

/**
 * @author cuisongxu
 * @date 2023/2/25 11:29
 */
public interface Component {

    /**
     * 获取表达式字符串描述
     * @return Statement
     */
    String getStatement();


    interface Builder {

        /**
         * 根据表达式类型和参数获取对应的ConfigId
         * @param type
         * @param params
         * @return
         */
        static String getConfigId(String type, String... params) {
            if(ExpressionComponentType.LOGIC.typeName().equals(type.toLowerCase())
                    && params.length >= ExpressionComponentType.LOGIC.requiredParamsNumber()) {
                return ExpressionComponentType.EXP.typeName();
            }else if(ExpressionComponentType.EXP.typeName().equals(type.toLowerCase())
                    && params.length >= ExpressionComponentType.EXP.requiredParamsNumber()) {
                CompareOperator operator = CompareOperator.fromNameOrSymbol(params[1]);
                if(null == operator) {
                    throw new IllegalArgumentException("Illegal CompareOperator");
                }
                return ExpressionComponentType.EXP.typeName().toUpperCase(Locale.ROOT) + "." + operator.name();
            }else {
                throw new IllegalArgumentException("Illegal Expression");
            }
        }

        /**
         * 生成表达式部件
         * @param type 表达式类型
         * @param params 表达式参数
         * @return 表达式部件
         * @throws IllegalArgumentException
         */
        Component build(String type, String... params) throws IllegalArgumentException;

        /**
         * 支持的配置ID列表
         * @return
         */
        List<String> supportedConfigIds();
    }
}
